package com.baizhi.service.impl;

import com.baizhi.dao.StudentDao;
import com.baizhi.dao.StudentToTagDao;
import com.baizhi.entity.Student;
import com.baizhi.entity.StudentToTag;
import com.baizhi.entity.Tag;
import com.baizhi.service.StudentService;
import com.baizhi.util.MybatisUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static com.baizhi.dao.StudentDao.PAGE_SIZE;

public class StudentServiceImpl implements StudentService {

    /* 添加学生 */
    @Override
    public void addStudent(Student student,String[] tags) {
        /*获取实现类对象*/
        StudentDao sd = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        /*根据getConstellation方法获取星座*/
        String star = getConstellation(student.getBirthday());
        /*将获取到的星座存放进学生对象中*/
        student.setStars(star);

        /*通过下面的方法可以分别获取dateStamp类型中的年月日*/
        Calendar date = Calendar.getInstance();
        date.setTime(student.getBirthday());
        /*获取年份*/
        int year = date.get(Calendar.YEAR);

        /*将对应年份的生肖存进student对象中*/
        student.setAttr(getYear(year));
        sd.insertStudent(student);
        /*获取所有tags*/
        //遍历tags
        StudentToTagDao studentToTagDao = (StudentToTagDao) MybatisUtil.getMapper(StudentToTagDao.class);
        for (String tagId : tags) {
            StudentToTag s = new StudentToTag(student.getId(),Integer.valueOf(tagId));
            studentToTagDao.insertSTT(s);
        }
        MybatisUtil.commit();
    }


    /* 删除 */
    @Override
    public void removeStudentById(Integer id) {
        /*获取实现类对象*/
        StudentDao sd = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        StudentToTagDao stto = (StudentToTagDao) MybatisUtil.getMapper(StudentToTagDao.class);
        stto.deleteSTTBySIdAndTId(id);
        sd.deleteStudentById(id);
        //提交
        MybatisUtil.commit();
    }


    /* 修改 */
    @Override
    public void changeStudent(Student student,String[] tIds) {
        /*获取实现类对象*/
        StudentDao sd = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        StudentToTagDao stto = (StudentToTagDao) MybatisUtil.getMapper(StudentToTagDao.class);
        /*先将原标签删除*/
        stto.deleteSTTBySIdAndTId(student.getId());
        /*在将新标签添加进去*/
        for (String tId : tIds) {
            stto.insertSTT(new StudentToTag(student.getId(),Integer.valueOf(tId)));
        }
        sd.updateStudent(student);
        //提交
        MybatisUtil.commit();
    }


    /* 查询所有 */
    @Override
    public List<Student> queryAllStudentByPage(String column,String value,Integer page) {
        /*获取student实现类对象*/
        StudentDao sd = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        /*查询的页码起始条数*/
        int pageIndex = (page - 1) * PAGE_SIZE;
        List<Student> students = sd.selectAllByPage(column,value,pageIndex);

        /*获取StudentToTag实现类对象*/
        StudentToTagDao stto = (StudentToTagDao) MybatisUtil.getMapper(StudentToTagDao.class);
        /*遍历查询到的学生*/
        for (Student student : students) {
            /*  根据学生id该查询学生的所有Tag   用于回显已选中的标签（根据学生id） */
            List<Tag> tags = stto.selectTags(student.getId());

            /*将查询到的tag存放进这个student对象中*/
            student.setTags(tags);
        }
        //提交
        MybatisUtil.close();
        return students;
    }


     /* 模糊查询的总人数 */
    @Override
    public Integer getCountByLike(String column, String value) {
        //获取实现类对象
        StudentDao sd = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        Integer count = sd.getCountByLike(column, value);
        //关闭资源
        MybatisUtil.close();
        return count;
    }


    /* 查询一条学生数据 */
    @Override
    public Student queryOne(Integer id) {
        /*获取student和StudentToTagDao实现类对象*/
        StudentDao sd = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        StudentToTagDao sttd = (StudentToTagDao) MybatisUtil.getMapper(StudentToTagDao.class);

        /*获取该id所对应的student对象和ta的所有tag*/
        List<Tag> tags = sttd.selectTags(id);
        Student student = sd.selectStudentById(id);
        /*将查询到的所有tag存进student对象中*/
        student.setTags(tags);
        //提交
        MybatisUtil.close();
        return student;
    }




    /* 根据小组id查询人数  */
    @Override
    public Integer queryNumberByGroupId(Integer groupId) {

        /*获取对象*/
        StudentDao sd = (StudentDao) MybatisUtil.getMapper(StudentDao.class);
        Integer groupSize = sd.selectStudentByGroupId(groupId);
        //关闭资源
        MybatisUtil.close();
        return groupSize;
    }



    /*从百度中获取的根据date类型属性查询星座*/
    public static String getConstellation(Date date) {
        String constellation = "";
        if (constellationList.isEmpty()) {
            fillData();
        }
        Calendar birthday = Calendar.getInstance();
        birthday.setTime(date);
        int month = birthday.get(Calendar.MONTH) + 1;
        int day = birthday.get(Calendar.DAY_OF_MONTH);
        switch (month) {
            case 1:
                //Capricorn 摩羯座（12月22日～1月20日）
                constellation = day <= 20 ? constellationList.get(11) : constellationList.get(0);
                break;
            case 2:
                //Aquarius 水瓶座（1月21日～2月19日）
                constellation = day <= 19 ? constellationList.get(0) : constellationList.get(1);
                break;
            case 3:
                //Pisces 双鱼座（2月20日～3月20日）
                constellation = day <= 20 ? constellationList.get(1) : constellationList.get(2);
                break;
            case 4:
                //白羊座 3月21日～4月20日
                constellation = day <= 20 ? constellationList.get(2) : constellationList.get(3);
                break;
            case 5:
                //金牛座 4月21～5月21日
                constellation = day <= 21 ? constellationList.get(3) : constellationList.get(4);
                break;
            case 6:
                //双子座 5月22日～6月21日
                constellation = day <= 21 ? constellationList.get(4) : constellationList.get(5);
                break;
            case 7:
                //Cancer 巨蟹座（6月22日～7月22日）
                constellation = day <= 22 ? constellationList.get(5) : constellationList.get(6);
                break;
            case 8:
                //Leo 狮子座（7月23日～8月23日）
                constellation = day <= 23 ? constellationList.get(6) : constellationList.get(7);
                break;
            case 9:
                //Virgo 处女座（8月24日～9月23日）
                constellation = day <= 23 ? constellationList.get(7) : constellationList.get(8);
                break;
            case 10:
                //Libra 天秤座（9月24日～10月23日）
                constellation = day <= 23 ? constellationList.get(8) : constellationList.get(9);
                break;
            case 11:
                //Scorpio 天蝎座（10月24日～11月22日）
                constellation = day <= 22 ? constellationList.get(9) : constellationList.get(10);
                break;
            case 12:
                //Sagittarius 射手座（11月23日～12月21日）
                constellation = day <= 21 ? constellationList.get(10) : constellationList.get(11);
                break;
        }
        return constellation;
    }

    private static ArrayList<String> constellationList = new ArrayList<>();//存放星座的集合

    private static void fillData() {
        constellationList.add(0, "水瓶座");
        constellationList.add(1, "双鱼座");
        constellationList.add(2, "白羊座");
        constellationList.add(3, "金牛座");
        constellationList.add(4, "双子座");
        constellationList.add(5, "巨蟹座");
        constellationList.add(6, "狮子座");
        constellationList.add(7, "处女座");
        constellationList.add(8, "天秤座");
        constellationList.add(9, "天蝎座");
        constellationList.add(10, "射手座");
        constellationList.add(11, "魔羯座");
    }

    /*从百度中获取的根据年份查询星座*/
    public String getYear(Integer year){
        if(year<1900){
            return "未知";
        }
        Integer start=1900;
        String [] years=new String[]{
                "鼠","牛","虎","兔",
                "龙","蛇","马","羊",
                "猴","鸡","狗","猪"
        };
        return years[(year-start)%years.length];
    }
}
